package com.groep21.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class Lengte extends Activity {
	private PhotoDbAdapter mDbHelper;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.graph);
		
		String color="&chco=0000FF,FF0000";
		String legenda="&chdl=Gemid.|Uw%20kind";
		String gemgroei = "54.5,57,59.4,62,64.5,66,68.5,70.5,71.5,73,74";
		
		mDbHelper = new PhotoDbAdapter(this);
        mDbHelper.open();
        String echtegroei = mDbHelper.fetchAllLengths();
        Log.d(this.toString(), "Lengtes: "
				+ echtegroei);
        mDbHelper.close();
        
        String data="&chd=t:"+gemgroei+"|"+echtegroei;
		
		final String url2 ="http://chart.apis.google.com/chart?cht=lc&chs=320x260&chxt=x,y&chxr=0,0,12|1,50,80&chds=50,80&chf=c,lg,0,FFE7C6,0,76A4FB,1"+data+color+legenda;
		
				View view = new View(null);
				Context context = view.getContext();
				Drawable image = ImageOperations(context,url2,"image.png");
				ImageView imgView = new ImageView(context);
				imgView = (ImageView)findViewById(R.id.image1);
				imgView.setImageDrawable(image);
				mDbHelper = new PhotoDbAdapter(this);
                mDbHelper.open();
                String lengths = mDbHelper.fetchAllLengths();
                Log.d(this.toString(), "Lengtes: "
						+ lengths);
                mDbHelper.close();

	}	

	private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Object fetch(String address) throws MalformedURLException,IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}

}
